package Middleware;

import java.awt.EventQueue;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

public class MiddlewareGUI extends JFrame {

	private JPanel contentPane;
	private JTextPane textPane_1;
	private StringBuilder logger;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MiddlewareGUI frame = new MiddlewareGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MiddlewareGUI() {
		setTitle("Middleware");
		getContentPane().setLayout(null);
		
		JTextPane textPane = new JTextPane();
		textPane.setBounds(22, 25, 382, 193);
		getContentPane().add(textPane);
		
		logger = new StringBuilder();
		Method logMethod = null;
		try {
			logMethod = this.getClass().getDeclaredMethod("Log", String.class);
			
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Middleware mw = new Middleware(logMethod, this);
		Thread mwThread = new Thread(mw);
		mwThread.start();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		
		JScrollPane jsp = new JScrollPane(textPane_1);
		jsp.setBounds(10, 30, 475, 500);
		contentPane.add(jsp);
		
		JLabel lblLogInformationMiddleware = new JLabel("Log information middleware");
		lblLogInformationMiddleware.setBounds(10, 11, 209, 14);
		contentPane.add(lblLogInformationMiddleware);
	}
	
	public void Log(String s) {
		logger.append(s);
		this.textPane_1.setText(logger.toString());
	}
}
